/*
 * Copyright (c) 2018-2999 湖南洛云网络科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.param.ScoreConfigParam;
import com.yami.shop.common.bean.SysConfig;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.util.Json;
import com.yami.shop.dao.SysConfigMapper;
import com.yami.shop.service.SysConfigService;
import lombok.AllArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author lgh
 */
@Service("sysConfigService")
@AllArgsConstructor
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements SysConfigService {

	private final SysConfigMapper sysConfigMapper;

	@Override
	@Caching(evict = {
			@CacheEvict(cacheNames="SysConfigObject",key="#key"),
			@CacheEvict(cacheNames="SysConfig",key="#key")
	})
	public void updateValueByKey(String key, String value) {
		sysConfigMapper.updateValueByKey(key, value);
	}

	@Override
	public void deleteBatch(Long[] ids) {
		sysConfigMapper.deleteBatch(ids);
	}

	@Override
	@Caching(evict = {
			@CacheEvict(cacheNames="SysConfigObject",key="#key"),
			@CacheEvict(cacheNames="SysConfig",key="#key")
	})
	public void removeSysConfig(String key) {
	}

	@Override
	//@Cacheable(cacheNames = "SysConfig", key = "#key")
	public String getValue(String key) {
		SysConfig config = sysConfigMapper.queryByKey(key);
		return config == null ? null : config.getParamValue();
	}

	@Override
	@Caching(evict = {
			@CacheEvict(cacheNames="SysConfigObject",key="#key"),
			@CacheEvict(cacheNames="SysConfig",key="#key")
	})
	public void saveOrUpdateSysConfigService(ScoreConfigParam scoreConfigParam, String key) {
		SysConfig config = new SysConfig();
		List<Integer> signInScore = scoreConfigParam.getSignInScore();
		if(CollectionUtils.isNotEmpty(signInScore)) {
			StringBuilder signsString = new StringBuilder();
			for (Integer score : signInScore) {
				signsString.append(score).append(StrUtil.COMMA);
			}
			signsString.deleteCharAt(signsString.length() - 1);
			scoreConfigParam.setSignInScoreString(signsString.toString());
			scoreConfigParam.setSignInScore(null);
		}
		String paramValue = Json.toJsonString(scoreConfigParam);
		config.setParamKey(Constant.SCORE_CONFIG);
		config.setParamValue(paramValue);
		config.setRemark("积分获取比例和积分抵现规则");
		if (sysConfigMapper.selectCount(new LambdaQueryWrapper<SysConfig>()
				.eq(SysConfig::getParamKey, config.getParamKey())) > 0) {
			sysConfigMapper.update(config,new LambdaUpdateWrapper<SysConfig>().eq(SysConfig::getParamKey,config.getParamKey()));
		}
		else{
			sysConfigMapper.insert(config);
		}
	}

	@Override
	//@Cacheable(cacheNames="SysConfigObject",key="#key")
	public <T> T  getSysConfigObject(String key, Class<T> clazz) {
		String value = getValue(key);
		if (StrUtil.isBlank(value)) {
			return null;
		}

		if(Objects.equals("java.lang.String",clazz.getName())){
			return (T)value;
		}else{
			return Json.parseObject(value, clazz);
		}
	}

}
